package com.demo.mantis.web.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
	private String greetings = null;
	
	public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.greetings = config.getInitParameter("greetings");
    }
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        var name = request.getParameter("name");
        response.setContentType("text/plain");
        response.getWriter().write(name + ", " + greetings);
        var context = request.getServletContext();
        var app = context.getInitParameter("appName");
        response.getWriter().write("\nGreetings from " + app);
    }
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        var name = request.getParameter("name");
        response.setContentType("text/plain");
        response.getWriter().write(name + ", " + greetings);
        var context = request.getServletContext();
        var app = context.getInitParameter("appName");
        response.getWriter().write("\nGreetings from " + app);
    }
	
	public void destroy() {
	    System.out.println("The game is over.");
	    super.destroy();
	}
}